package com.mine.modules.vm.common.utils;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import com.mine.common.utils.DateUtils;
import com.mine.common.utils.http.HttpClientResult;
import com.mine.common.utils.http.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.util.StrUtil;

@Component
public class SensorUtil {

    @Value("${sensor.http.host}")
    private String host;

    @Value("${sensor.http.tokenPath}")
    private String tokenPath;

    @Value("${sensor.http.pointPath}")
    private String pointPath;

    @Value("${sensor.http.queryDataPath}")
    private String queryDataPath;

    @Value("${sensor.http.queryAggregation}")
    private String queryAggregation;

    @Value("${sensor.http.queryTime}")
    private String queryTime;

    @Value("${sensor.http.userName}")
    private String userName;

    @Value("${sensor.http.password}")
    private String password;

    public String getToken() {
        Map<String, String> prams = new HashMap<String, String>();
        String token = "";
        try {
            prams.put("userName", userName);
            prams.put("password", password);
            String result = HttpUtil.doPostJson(String.format("%s%s", host, tokenPath), JSON.toJSONString(prams));
            if (StrUtil.isNotBlank(result)) {
                JSONObject res = JSONObject.parseObject(result);
                if (res.getInteger("code") == 200 && res.getBoolean("success")) {
                    token = res.getString("result");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    public JSONArray getAllPoint() {
        Map<String, String> heards = new HashMap<String, String>();
        heards.put("Authorization", getToken());
        JSONArray list = null;
        try {
            HttpClientResult result = HttpUtil.doGet(String.format("%s%s", host, pointPath), heards, null);
            if (result.getCode() == 200) {
                if (StrUtil.isNotBlank(result.getContent())) {
                    JSONObject content = JSONObject.parseObject(result.getContent());
                    if (content.getInteger("errcode") == 0) {
                        if (StrUtil.isNotBlank(content.getString("data"))) {
                            list = JSONArray.parseArray(content.getString("data"));
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public JSONObject valueDataQuery(String tableName, String beginTime, String endTime, String ofs, String ps) {
        Map<String, String> heards = new HashMap<String, String>();
        heards.put("Authorization", getToken());
        JSONObject data = null;
        try {
            Map<String, String> param = new HashMap<String, String>();
            param.put("tableName", tableName);
            if(StringUtils.isNotBlank(beginTime)){
                param.put("beginTime", String.valueOf(DateUtils.parseDate(beginTime, "yyyy-MM-dd HH:mm:ss").getTime()));
            }
            if(StringUtils.isNotBlank(endTime)){
                param.put("endTime", String.valueOf(DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm:ss").getTime()));
            }
            param.put("ofs",ofs);
            param.put("ps",ps);
            HttpClientResult result = HttpUtil.doGet(String.format("%s%s", host, queryDataPath), heards, param);
            if (result.getCode() == 200) {
                if (StrUtil.isNotBlank(result.getContent())) {
                    JSONObject content = JSONObject.parseObject(result.getContent());
                    if (content.getInteger("errcode") == 0) {
                        if (StrUtil.isNotBlank(content.getString("data"))) {
                            data = JSONObject.parseObject(content.getString("data"));
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public JSONArray queryAggregation(String tableName, String beginTime, String endTime) {
        Map<String, String> heards = new HashMap<String, String>();
        heards.put("Authorization", getToken());
        JSONArray list = null;
        try {
            Map<String, String> param = new HashMap<String, String>();
            param.put("functionName", "avg");
            param.put("tableName", tableName);
            if(StringUtils.isNotBlank(beginTime)){
                param.put("beginTime", String.valueOf(DateUtils.parseDate(beginTime, "yyyy-MM-dd HH:mm:ss").getTime()));
            }
            if(StringUtils.isNotBlank(endTime)){
                param.put("endTime", String.valueOf(DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm:ss").getTime()));
            }
            param.put("timeZone", "1s");
            HttpClientResult result = HttpUtil.doGet(String.format("%s%s", host, queryAggregation), heards, param);
            if (result.getCode() == 200) {
                if (StrUtil.isNotBlank(result.getContent())) {
                    JSONObject content = JSONObject.parseObject(result.getContent());
                    if (content.getInteger("errcode") == 0) {
                        if (StrUtil.isNotBlank(content.getString("data"))) {
                            JSONObject data = JSONObject.parseObject(content.getString("data"));
                            if(StringUtils.isNotBlank(data.getString("list"))){
                                list = JSONArray.parseArray(data.getString("list"));
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public JSONArray queryTime(String tableName, String dateTime) {
        Map<String, String> heards = new HashMap<String, String>();
        heards.put("Authorization", getToken());
        JSONArray list = null;
        try {
            Map<String, String> param = new HashMap<String, String>();
            param.put("tableName", tableName);
            if(StringUtils.isNotBlank(dateTime)){
                param.put("dateTime", String.valueOf(DateUtils.parseDate(dateTime, "yyyy-MM-dd HH:mm:ss").getTime()));
            }
            HttpClientResult result = HttpUtil.doGet(String.format("%s%s", host, queryTime), heards, param);
            if (result.getCode() == 200) {
                if (StrUtil.isNotBlank(result.getContent())) {
                    JSONObject content = JSONObject.parseObject(result.getContent());
                    if (content.getInteger("errcode") == 0) {
                        if (StrUtil.isNotBlank(content.getString("data"))) {
                            JSONObject data = JSONObject.parseObject(content.getString("data"));
                            if(StringUtils.isNotBlank(data.getString("list"))){
                                list = JSONArray.parseArray(data.getString("list"));
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

}
